package util

import (
	"github.com/google/uuid"
	"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
	httpconfig "github.com/huaweicloud/huaweicloud-sdk-go-v3/core/config"
	ivs "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/ivs/v2"
	ivsmodel "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/ivs/v2/model"
	"nft-server/config"
	"nft-server/errcode"
	"nft-server/logger"
)

var (
	ivsEndpoint = config.GetConfig().IVSConfig.Endpoint
	ivsAK       = config.GetConfig().IVSConfig.AK
	ivsSK       = config.GetConfig().IVSConfig.SK
)

const (
	AuthenticationSuccessCode = 1000
)

type AuthenticationRes struct {
	Result string `json:"VerificationResult"`
	Msg    string `json:"VerificationMessage"`
	Code   int    `json:"VerificationCode"`
}

// 调用IVS接口
func CallIVSService(verificationName string, verificationID string) (AuthenticationRes, error) {

	// 使用默认配置

	auth := getCredential(ivsAK, ivsSK)
	client := getClient(auth, ivsEndpoint)

	request := &ivsmodel.DetectExtentionByNameAndIdRequest{}

	var listReqDataIvsExtentionByNameAndIdRequestBodyData = []ivsmodel.ExtentionReqDataByNameAndId{
		{
			VerificationName: verificationName,
			VerificationId:   verificationID,
		},
	}
	// 构造request
	dataBody := &ivsmodel.IvsExtentionByNameAndIdRequestBodyData{
		ReqData: &listReqDataIvsExtentionByNameAndIdRequestBodyData,
	}
	uuidMetaMeta := createUuid()
	metaBody := &ivsmodel.Meta{Uuid: &uuidMetaMeta}

	request.Body = &ivsmodel.IvsExtentionByNameAndIdRequestBody{
		Data: dataBody,
		Meta: metaBody,
	}

	response, err := client.DetectExtentionByNameAndId(request)
	authRes := AuthenticationRes{}
	if err != nil {
		//这里应该还要对错误进行处理,需要对照错误代码看看
		return authRes, err
	}
	ivsResBody := (*response.Result.RespData)[0]
	authRes.Code = int(*ivsResBody.VerificationCode)
	authRes.Result = *ivsResBody.VerificationResult
	authRes.Msg = *ivsResBody.VerificationMessage

	return authRes, nil

}

// 生成uuid
func createUuid() string {
	uuidMetaMeta := uuid.New().String()
	return uuidMetaMeta
}

// 通过AK SK获得证书
func getCredential(ak, sk string) *basic.Credentials {
	return basic.NewCredentialsBuilder().
		WithAk(ak).
		WithSk(sk).
		Build()
}

// 生成client
func getClient(auth *basic.Credentials, ivsEndpoint string) *ivs.IvsClient {
	httpConfiguration := httpconfig.DefaultHttpConfig()
	// 这句话在上线前要把true改成false
	httpConfiguration.WithIgnoreSSLVerification(true)
	return ivs.NewIvsClient(
		ivs.IvsClientBuilder().
			WithEndpoint(ivsEndpoint).
			WithCredential(auth).WithHttpConfig(httpConfiguration).
			Build())
}

func ParserAuthenticationErrorCode(statusCode int) (errCode errcode.Status) {
	switch statusCode {
	case 2001:
		logger.Error("User Authentication error: Mismatch")
		return errcode.E.Base.ErrorMessage.WithMessage("姓名与身份证号码不一致。")
	case 2002:
		logger.Error("User Authentication error: Mismatch")
		return errcode.E.Base.ErrorMessage.WithMessage("人脸与身份证信息不符。")
	case 2003:
		logger.Error("User Authentication error: invalid format")
		return errcode.E.Base.ErrorMessage.WithMessage("人脸图像质量或格式问题。")
	case 2004:
		logger.Error("User Authentication error: fail to detect face")
		return errcode.E.Base.ErrorMessage.WithMessage("未检测到人脸或检测到多张人脸。")
	case 2006:
		logger.Error("User Authentication error: fail to detect identification photo")
		return errcode.E.Base.ErrorMessage.WithMessage("证件照不存在")
	case 3000:
		logger.Error("User Authentication error: no source")
		return errcode.E.Base.ErrorMessage.WithMessage("认证中心数据源无此身份证信息")
	default:
		logger.Error("User Authentication error: abnormal error")
		return errcode.E.Base.ErrorMessage.WithMessage("异常错误")
	}
}
